package com.yh.rent.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.yh.rent.dao.HouseDao;
import com.yh.rent.entity.Condition;
import com.yh.rent.entity.House;
import com.yh.rent.utils.HibernateUtil;

public class HouseDaoImpl implements HouseDao {
	Session session = null;

	@Override
	public List<House> getAllHouseByPage(Integer pageIndex, Integer pageSize) {
		session = HibernateUtil.getCurrentSession();
		Transaction ts = session.beginTransaction();
		String hql = "from House house order by house.pubdate desc";
		Query query = session.createQuery(hql);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageIndex - 1) * pageSize);
		@SuppressWarnings("unchecked")
		List<House> list = query.list();
		ts.commit();
		return list;
	}

	@Override
	public House getHouseById(Integer id) {
		session = HibernateUtil.getCurrentSession();
		Transaction ts = session.beginTransaction();
		Query query = session.createQuery("from House house where id = :id ");
		query.setParameter("id", id);
		House house = (House) query.uniqueResult();
		ts.commit();
		return house;
	}

	@Override
	public int getTotalCount() {
		session = HibernateUtil.getCurrentSession();
		Transaction ts = session.beginTransaction();
		Query query = session.createQuery("select count(house.id) from House house");
		Long count = (Long) query.uniqueResult();
		int count2 = count.intValue();
		ts.commit();
		return count2;
	}

	@Override
	public List<House> getHouseByCondition(Integer pageIndex, Integer pageSize, Condition condition) {
		session = HibernateUtil.getCurrentSession();
		Transaction ts = session.beginTransaction();
		String hql = "from House house where 1 = 1 ";
		// 获取条件
		String titleStr = condition.getTitleStr();
		Double minPrice = condition.getMinPrice();
		Double maxPrice = condition.getMaxPrice();
		Double minFloorage = condition.getMinFloorage();
		Double maxFloorage = condition.getMaxFloorage();
		Integer streetId = condition.getStreetId() ;
		Integer typeId = condition.getTypeId();
		// 给hql语句加上条件
		hql += titleStr != null ? " and title like '%" + titleStr + "%'" : "";
		hql += minPrice != null ? " and price >= :minPrice " : "";
		hql += maxPrice != null ? " and price <= :maxPrice " : "";
		hql += minFloorage != null ? " and floorage >= :minFloorage "  : "";
		hql += maxFloorage != null ? " and floorage <= :maxFloorage "  : "";
		hql += streetId != null ? " and house.street.id = :streetId " : "";
		hql += typeId != null ? " and house.type.id = :typeId " : "";
		hql += " order by house.pubdate desc ";
		// 执行hql语句
		Query query = session.createQuery(hql);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageIndex - 1) * pageSize);
		query.setProperties(condition) ;
		@SuppressWarnings("unchecked")
		List<House> list = query.list();
		ts.commit();
		return list;
	}

	@Override
	public int getCountByConditon(Condition condition) {
		session = HibernateUtil.getCurrentSession();
		Transaction ts = session.beginTransaction();
		String hql = "select count(*) from House house where 1 = 1 ";
		// 获取条件
		String titleStr = condition.getTitleStr();
		Double minPrice = condition.getMinPrice();
		Double maxPrice = condition.getMaxPrice();
		Double minFloorage = condition.getMinFloorage();
		Double maxFloorage = condition.getMaxFloorage();
		Integer streetId = condition.getStreetId();
		Integer typeId = condition.getTypeId();
		// 给hql语句加上条件
		hql += titleStr != null ? " and title like '%" + titleStr + "%'" : "";
		hql += minPrice != null ? " and price >= :minPrice " : "";
		hql += maxPrice != null ? " and price <= :maxPrice " : "";
		hql += minFloorage != null ? " and floorage >= :minFloorage "  : "";
		hql += maxFloorage != null ? " and floorage <= :maxFloorage "  : "";
		hql += streetId != null ? " and house.street.id = :streetId " : "";
		hql += typeId != null ? " and house.type.id = :typeId " : "";
		// 执行hql语句
		Query query = session.createQuery(hql);
		query.setProperties(condition);
		Long count = (Long) query.uniqueResult();
		int count2 = count.intValue();
		ts.commit();
		return count2;
	}

}
